<template>
    <div class="app-header">
        <div class="title" @click="useMean.setIsCollapse(!useMean.isCollapse)"><el-icon>
                <component :is="useMean.isCollapse ? 'Expand' : 'Fold'" />
            </el-icon></div>
        <div class="left">员工管理系统</div>
        <div class="right">
            <div class="tiem">
                <div class="name">{{ userStore.userInfo.username || userStore.userInfo.staff_name }}</div>
                <div>{{ date }}</div>
            </div>
            <el-dropdown class="tags-dropdown">
                <el-avatar :size="35" :src="avator" />
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item @click="toCenter">个人详情</el-dropdown-item>
                        <el-dropdown-item @click="loginOut">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>
    </div>
</template>

<script setup>
import { useUserStore } from '@/store/user';
import { useTagsViewStore } from '@/store/tagsView';
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
import { dayjs } from 'element-plus';
import { useRouter } from 'vue-router';
import { getUserInfo } from '@/api/user'
import { useMeanStore } from '@/store/menus';

const router = useRouter()
const userStore = useUserStore()
const tagsStore = useTagsViewStore()
const useMean = useMeanStore()
const date = ref(null)
const avator = computed(() => userStore.avator)

const loginOut = () => {
    userStore.remove()
    tagsStore.delAllViews()
    location.reload()
}

const getUser = async () => {
    const res = await getUserInfo();
    userStore.setAvatar(res.data.avator || '');
    userStore.setInfo(res.data || '');
}

const getTime = () => {
    setInterval(getTimeInterval, 1000)
}

const getTimeInterval = () => {
    return date.value = dayjs().format('YYYY-MM-DD HH:mm:ss')
}

const toCenter = () => {
    router.push('/my/center')
}

onMounted(() => {
    getTimeInterval()
    getTime();
    getUser()
})

onUnmounted(() => {
    clearInterval(getTimeInterval)
})
</script>

<style lang="scss" scoped>
.app-header {
    width: 100%;
    height: 50px;
    background: rgba($color: #000000, $alpha: .5);
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
        min-width: 220px;
        font-size: 30px;
        line-height: 40px;
        color: #fff;
        box-sizing: border-box;
        padding: 5px 0 0 15px;
    }

    .left {
        min-width: 220px;
        font-size: 20px;
        text-align: center;
    }

    .right {
        width: auto;
        min-width: 220px;
        height: 100%;
        margin-right: 40px;
        display: flex;
        align-items: center;

        .tiem {
            // margin-top: 15px;
            margin-right: 20px;

            .name {
                text-align: end;
            }
        }
    }
}
</style>